<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/7
 * Time: 上午10:52
 */
class User_model extends CI_Model
{
	/**
	* 注册会员
	**/
	public function reg_user($phone,$rand_str,$password,$register_ip)
	{
		//事务开始
		$this->db->trans_start();

		$user_data = array(
		'login_name' => '',
		'rand_str' => $rand_str,
		'password' => $password,
		'mobile' => $phone,
		'is_reg_xmpp' => 0,
		'sex' => 0,
		'register_ip' => $register_ip,
		'status' => 1,
		'add_time' => time(),
		'school_id' => $this->school_id,
		'edit_time' => 0,
		);
		$this->db->insert('user', $user_data);
		$user_id = $this->db->insert_id();

		//user_account
		$user_account = array(
		'user_id' => $user_id,
		'balance' => 0,
		'integral' => 0,
		'commission_total' => 0
		);
		$this->db->insert('user_account', $user_account);

		$this->db->trans_complete();
		return $user_id;
	}

	/**
	* 根据用户id获取user_account
	*/
	public function get_user_account_by_id($user_id)
	{
		$sql = "SELECT * FROM {$this->db->dbprefix}user_account where user_id = '{$user_id}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	/**
	 * 获取我的奖励
	 * @param [type] $user_id [description]
	 */
	function get_commission_by_user($user_id){
		$sql = "select * from {$this->db->dbprefix}user_commission where user_id = $user_id";
		return $this->db->query($sql)->result_array();
	}

	/**
	 * 获取一级同学
	 * @param  [type] $parent [description]
	 * @return [type]         [description]
	 */
	function get_user_by_parent1($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent1 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	/**
	 * 获取二级同学
	 * @param  [type]  $parent [description]
	 * @param  integer $limit  [description]
	 * @param  integer $offset [description]
	 * @return [type]          [description]
	 */
	function get_user_by_parent2($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent2 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	/**
	 * 获取三级同学
	 * @param  [type]  $parent [description]
	 * @param  integer $limit  [description]
	 * @param  integer $offset [description]
	 * @return [type]          [description]
	 */
	function get_user_by_parent3($parent,$limit =0,$offset =10){
		$this->db->trans_start();

		$sql = "SELECT SQL_CALC_FOUND_ROWS u.id,u.login_name,u.nick,u.mobile,u.sex,u.add_time,u.is_vip,
			(select count(0) from {$this->db->dbprefix}user where parent1 = u.id and school_id = {$this->school_id}) as parent1_count,
			(select count(0) from {$this->db->dbprefix}user where parent2 = u.id and school_id = {$this->school_id}) as parent2_count,
			(select count(0) from {$this->db->dbprefix}user where parent3 = u.id and school_id = {$this->school_id}) as parent3_count
		 FROM {$this->db->dbprefix}user u where parent3 = {$parent} and school_id = {$this->school_id} LIMIT $limit, $offset;";

		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}



	/**
	* 根据手机号获取用户信息
	*/
	function get_user_by_phone($phone){
		$sql = "SELECT usr.*,stu.name FROM {$this->db->dbprefix}user usr left join {$this->db->dbprefix}o2o_student stu on stu.user_id=usr.id where mobile = '{$phone}' and school_id = {$this->school_id} LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	/**
	* 根据主键id获取用户信息
	*/
	function get_user_by_id($user_id){
		$sql = "SELECT * FROM {$this->db->dbprefix}user where id = '{$user_id}' LIMIT 1";
		return $this->db->query($sql)->row_array();
	}

	function get_ask_user($limit = 4){
		$sql = "SELECT id, nick, head_image_url FROM {$this->db->dbprefix}user ORDER BY login_time DESC LIMIT $limit";
		return $this->db->query($sql)->result_array();
	}



	function get_school(){
		$url_web = 'http://'.$_SERVER['HTTP_HOST'];
		$sql = "SELECT * from {$this->db->dbprefix}o2o_school where url_web = '".$url_web."'";
		$result = $this->db->query($sql)->row_array();
		return $result;
	}

	function get_user_commission($order_id){

		$sql = "SELECT * FROM {$this->db->dbprefix}user_commission WHERE order_id = '{$order_id}'";
		return $this->db->query($sql)->result_array();
	}

	function get_user_share_list($limit = 0, $offset = 10){
		$sql = "SELECT SQL_CALC_FOUND_ROWS us.*,c.type,c.title,s.school_name FROM {$this->db->dbprefix}user_share AS us ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}o2o_school AS s ON(s.id=us.school_id) ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}course AS c ON(c.id=us.course_id) ";
		$sql .= "WHERE user_id='{$this->user_data['id']}' LIMIT $limit, $offset";
		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();

		return array("total_count" => $row_num['row_num'], "result" => $result);
	}
}
